/**
 * 
 */
package com.deep.test.event;

import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author hudepin 负责注册和分发event
 *
 *         2018年2月2日下午12:53:33
 */
public class ListenerRegister {
	private CopyOnWriteArrayList<ValueChangeListener> listeners = new CopyOnWriteArrayList<ValueChangeListener>();
	public synchronized void addListener(ValueChangeListener a) {
		listeners.add(a);
	}

	public synchronized void removeListener(ValueChangeListener a) {
		listeners.remove(a);
	}

	@SuppressWarnings("unchecked")
	public void fireAEvent(ValueChangeEvent evt) {
		CopyOnWriteArrayList<ValueChangeListener> currentListeners = null;
		synchronized (this) {
			currentListeners = (CopyOnWriteArrayList<ValueChangeListener>) listeners.clone();
		}
		for (int i = 0; i < currentListeners.size(); i++) {
			ValueChangeListener listener = (ValueChangeListener) currentListeners.get(i);
			listener.performed(evt);
		}
	}

}
